// 子模块moduleA路径：store/modules/moduleA.js 
export default {
    namespaced:true,
    state: {
        cartList:uni.getStorageSync('cartList')||[]
    },
    getters: {

    },
    mutations: {
         // 给购物车 新增商品元素 payload={value:要添加的商品对象}
        cartAdd(state,payload){
            const index = state.cartList.findIndex(item => item.goods_id===payload.value.goods_id)
            console.log(15,index);
            if(index === -1){
                // 不存在 push新的元素
                // 要自己设置一个购买数量进去   添加一个checked属性
                state.cartList.push({...payload.value,num:1,checked:true});  
            }else{
                // 有就加数量
                state.cartList[index].num++
            }    
            console.log(state.cartList);
            //添加一份到本地存储中
            uni.setStorageSync('cartList', state.cartList)
        },
        // 商品选中
        cartCheck(state, payload){
            state.cartList[payload.index].checked = !state.cartList[payload.index].checked
            uni.setStorageSync('cartList', state.cartList)
        },
        // 全选
        cartCheckAll(state, payload){
            state.cartList.forEach(item => item.checked=payload.checked)
            uni.setStorageSync('cartList', state.cartList)
        },
        // 编辑商品数量
        cartNumEdit(state, payload){
            state.cartList[payload.index].num += payload.unit
            uni.setStorageSync('cartList', state.cartList)    
        },
        // 删除商品时
        cartRemove(state, payload){
            state.cartList.splice(payload.index,1)
            uni.setStorageSync('cartList', state.cartList)    
        }
    },
    actions: { 

    }
}